﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using CommandLib;

namespace Services.Controllers
{
    public class ContentController : Controller
    {
        //
        // GET: /Content/

        public ActionResult Images(int type, int id, string fileName)
        {                       
            if (type == Constant.IMAGE_ARTWORK)
            {
                ArtWork artWork = new ArtWorkBL().GetById(id);
                //var imagePath = Path.Combine(Config.FolderImage, artWork.ArtworkFile);            
                //if (!System.IO.File.Exists(imagePath))
                //{
                //    imagePath = Path.Combine(Config.FolderImage, "default.png");
                //}
                //var imgResponse = Image.FromFile(imagePath);

                //var temp = new MemoryStream();
                //imgResponse.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
                //return File(temp.ToArray(), Utils.GetMimeType(imagePath));     
                var tg = @"http://art.findartinfo.com/" + artWork.ArtworkFile;
                using (var client = new WebClient())
                {
                    byte[] image = client.DownloadData(tg);
                    //return File(image, "image/jpg");
                    return File(image, Utils.GetMimeType(artWork.ArtworkFile));
                }
            }
            if (type == Constant.IMAGE_SLIDESHOW)
            {
                SlideShowDetail slideShowDetail = new SlideShowDetailBL().GetById(id);
                string imagePath = Path.Combine(slideShowDetail.ImageUrl,fileName);
                if (!System.IO.File.Exists(imagePath))
                {
                    imagePath = Path.Combine(Config.FolderImage, "default.png");
                }
                Image imgResponse = Image.FromFile(imagePath);

                var temp = new MemoryStream();
                imgResponse.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
                return File(temp.ToArray(), Utils.GetMimeType(imagePath));     
            }
            if (type == Constant.IMAGE_WHATWEDO)
            {
                ImageWhatWeDo imageWhatWeDo = new ImageWhatWeDoBL().GetTop();
                string imagePath = string.Empty;
                if(string.Equals(fileName,imageWhatWeDo.ImageArtist))
                {
                    imagePath = Path.Combine(imageWhatWeDo.ImageArtistUrl, fileName);
                }
                if (string.Equals(fileName, imageWhatWeDo.ImageArtPrice))
                {
                    imagePath = Path.Combine(imageWhatWeDo.ImageArtPriceUrl, fileName);
                }
                if (string.Equals(fileName, imageWhatWeDo.ImageAuctionCalendar))
                {
                    imagePath = Path.Combine(imageWhatWeDo.ImageAuctionCalendarUrl, fileName);
                }
                if (!System.IO.File.Exists(imagePath))
                {
                    imagePath = Path.Combine(Config.FolderImage, "default.png");
                }
                Image imgResponse = Image.FromFile(imagePath);

                var temp = new MemoryStream();
                imgResponse.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
                return File(temp.ToArray(), Utils.GetMimeType(imagePath));
            }
            if (type == Constant.PROJECT_IMAGE_ARTIST || type == Constant.PROJECT_IMAGE_ARTWORK || type == Constant.PROJECT_IMAGE_SIGNATURES)
            {
                ImageLastProject imageLastProject = new ImageLastProjectBL().GetById(id);
                string imagePath = Path.Combine(imageLastProject.ImageUrl, fileName);                
                
                if (!System.IO.File.Exists(imagePath))
                {
                    imagePath = Path.Combine(Config.FolderImage, "default.png");
                }
                Image imgResponse = Image.FromFile(imagePath);

                var temp = new MemoryStream();
                imgResponse.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
                return File(temp.ToArray(), Utils.GetMimeType(imagePath));
            }
            return File("",".jpg");
        }
    }
}
